// this is to implement the PDBsum-like diagrams of assemblies and interfaces
// obtain json for layout of circles, ie components and connections for each component
// component  = name, size, location in 2D, colour (or random color to be used)
// connection = to which component? size of connection-start, size of connection-end

// a banded circle is one which show radial bands at its periphery
function BandedCircle(configs) {
	var me = this;
	for(cfk in configs) me[cfk] = configs[cfk];
	me.angle360 = function(angle) {
		while(angle < 0) angle += 360;
		angle = angle % 360;
		return angle;
	}
	me.findAngle360 = function(p1, p2) { // angle made by p2-p1
		var dx = p2[0]-p1[0]; var dy = p2[1]-p1[1];
		var angle = Math.atan( Math.abs(dy)/Math.abs(dx) ) * 180/Math.PI;
		if(dx >= 0) {
			if(dy >= 0) return angle;
			else return 360-angle;
		}
		else {
			if(dy >= 0) return 180-angle;
			else return 180+angle;
		}
	}
	me.arcpath = function(cx, cy, radius, fromangle, toangle, reverseAngle) {
		if(fromangle == toangle) fromangle = fromangle-1;
		var large_arc_flag = 1;
		if(me.angle360(toangle-fromangle) < 180) large_arc_flag = 0;
		if(reverseAngle!=null && large_arc_flag==1) { large_arc_flag = 0; }
		else if(reverseAngle!=null && large_arc_flag==0) { large_arc_flag = 1; }
		var sweep_flag = 1;
		if(fromangle > toangle) sweep_flag = 0;
		fromangle = Math.PI * (fromangle % 360) / 180;
		toangle = Math.PI * (toangle % 360) / 180;
		var startx = cx + radius*Math.cos(fromangle);
		var starty = cy + radius*Math.sin(fromangle);
		var endx = cx + radius*Math.cos(toangle);
		var endy = cy + radius*Math.sin(toangle);
		var thepath = "M" + startx + " " + starty + "A" + radius + " " + radius + " 0 "+large_arc_flag+" "+sweep_flag+" " + endx + " " + endy;
		return [thepath, [startx,starty], [endx,endy]];
	};
	me.randomColor = function() {
		var cols = '0123456789abcdef';
		var color = '#';
		for(var ci=0; ci < 6; ci++) color += cols[Math.floor(Math.random()*16)];
		return color;
	};
	me.render = function() {
		me.paper = Raphael( document.getElementById(me.container), me.canvasWidth, me.canvasHeight );
		//me.paper.circle(200, 200, 80); me.paper.path("M0 0L200 200L500 500");
		for(var ci=0; ci < me.components.length; ci++) {
			var acomp = me.components[ci];
			var cen = acomp.place; var rad = acomp.size;
			me.paper.circle(cen[0], cen[1], rad);
			for(var i=0; i < 0; i++) {
				var pierad = Math.random()*rad/2 + rad/2;
				var startangle = Math.random()*360;
				var endangle = startangle + Math.random()*360;
				var width = 5;//Math.random()*rad/2;
				me.renderPie(cen[0], cen[1], pierad, startangle, endangle, width, me.randomColor());
			}
			// check connections and draw pies
			var conns = me.connections[ci];
			for(var ni=0; ni < conns.length; ni++) {
				var acon = conns[ni];
				var ck = acon.to;
				var c2 = me.components[ck].place;
				me.paper.path("M" + cen[0] + " " + cen[1] + "L" + c2[0] + " " + c2[1]);
				var angle = me.findAngle360(cen, c2);
				me.renderPie(cen[0], cen[1], rad, angle-10, angle+10, 10, me.randomColor())
			}
		}
	};
	me.renderPie = function(cenx, ceny, rad, startangle, endangle, width, fillcolor) {
		var path1 = me.arcpath(cenx,ceny, rad, startangle, endangle, null);
		var path2 = me.arcpath(cenx,ceny, rad-width, endangle, startangle, 1);
		var p1 = path1[0], s1 = path1[1], e1 = path1[2];
		var p2 = path2[0], s2 = path2[1], e2 = path2[2];
		me.paper.path( p1 + "L" + s2[0]+" "+s2[1] + p2 + "L" + s1[0]+" "+s1[1] ).attr({'fill':fillcolor});
	};
	return me;
}
